/*
 * Created by JFormDesigner on Thu Nov 06 17:38:33 CST 2025
 */

package com.yeban.bams.server;

import com.yeban.bams.server.serversocket.BankServer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author yeban
 */
public class ServerPanel extends JPanel {
    private ATMServer atm;
    private BankServer bankServer;

    public ServerPanel(ATMServer atm) {
        this.atm = atm;
        initComponents();
    }

    // 【开启服务】按钮
    private void open(ActionEvent e) {
        if(bankServer != null) {
            JOptionPane.showMessageDialog(this, "服务器已启动", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        // 启动独立线程，防止Swing页面卡死
        new Thread(() -> {
            bankServer = new BankServer();  // 实例化BankServer
            bankServer.startServer();
        }).start();

        JOptionPane.showMessageDialog(this, "服务器启动成功，端口：8888", "成功", JOptionPane.INFORMATION_MESSAGE);
    }

    // 【关闭服务】按钮
    private void close(ActionEvent e) {
        if (bankServer == null) {
            JOptionPane.showMessageDialog(this, "服务器未启动", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        bankServer.stopServer();
        bankServer = null;
        JOptionPane.showMessageDialog(this, "服务器已关闭", "提示", JOptionPane.INFORMATION_MESSAGE);
    }

    // 【开启上帝视角】按钮
    private void god(ActionEvent e) {
        atm.rankPanel.clear(); // 进入RankPanel前先清空目标页面
        CardLayout cardLayout = (CardLayout) atm.getContentPane().getLayout();
        cardLayout.show(atm.getContentPane(), "rankPanel");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        serverTitle = new JLabel();
        openButton = new JButton();
        closeButton = new JButton();
        godButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- serverTitle ----
        serverTitle.setText("\u6a21\u62df  BAMS  \u670d\u52a1\u5668");
        serverTitle.setHorizontalAlignment(SwingConstants.CENTER);
        serverTitle.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 30));
        serverTitle.setForeground(Color.red);
        add(serverTitle);
        serverTitle.setBounds(65, 110, 370, 185);

        //---- openButton ----
        openButton.setText("\u5f00\u542f\u670d\u52a1");
        openButton.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 18));
        openButton.addActionListener(e -> open(e));
        add(openButton);
        openButton.setBounds(100, 330, 120, 70);

        //---- closeButton ----
        closeButton.setText("\u5173\u95ed\u670d\u52a1");
        closeButton.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 18));
        closeButton.addActionListener(e -> close(e));
        add(closeButton);
        closeButton.setBounds(275, 330, 120, 70);

        //---- godButton ----
        godButton.setText("\u5f00\u542f\u4e0a\u5e1d\u89c6\u89d2");
        godButton.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 18));
        godButton.addActionListener(e -> god(e));
        add(godButton);
        godButton.setBounds(322, 465, 160, 55);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel serverTitle;
    private JButton openButton;
    private JButton closeButton;
    private JButton godButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
